package cn.edu.cdp.dumpling.controller;

import cn.edu.cdp.dumpling.dto.DishDto;
import cn.edu.cdp.dumpling.entity.Business;
import cn.edu.cdp.dumpling.entity.Category;
import cn.edu.cdp.dumpling.entity.Dish;
import cn.edu.cdp.dumpling.service.BusinessService;
import cn.edu.cdp.dumpling.service.CategoryService;
import cn.edu.cdp.dumpling.service.DishService;
import cn.edu.cdp.dumpling.util.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 菜品管理：根据类别查看菜品资料、根据菜品名称查询详细信息；增加、删除、修改菜品资料。
 */
@RestController
//@CrossOrigin
@RequestMapping("/dishes")
public class DishController {
    private final DishService dishService;

    public DishController(DishService dishService, CategoryService categoryService, BusinessService businessService) {
        this.dishService = dishService;
    }


    // 查询
    @GetMapping
    public R list(@RequestParam(value = "category", required = false) String category,
                  @RequestParam(value = "dish", required = false) String dish) {
        List<DishDto> list = dishService.list(category, dish);
        if (list == null || list.isEmpty())
            return R.error("查询失败");
        else
            return R.success("查询成功", list);
    }


    // 增加
    @PostMapping
    public R save(@RequestBody Dish dish) {
        if (dishService.save(dish))
            return R.success("添加成功", null);
        else
            return R.error("添加失败");
    }

    // 删除
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Integer id) {
        if (dishService.removeById(id))
            return R.success("删除成功", null);
        else
            return R.error("删除失败");
    }

    // 修改菜品资料
    @PutMapping
    public R updateById(@RequestBody Dish dish) {
        if (dishService.updateById(dish))
            return R.success("修改成功", null);
        else
            return R.error("修改失败");
    }
}
